package com.itheima.dao.impl;

import com.itheima.dao.BaseUserDao;
import com.itheima.domain.Pow;
import com.itheima.domain.User;
import com.itheima.util.JDBCUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao_JDBC implements BaseUserDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserDao_JDBC.class);
    @Override
    public boolean addUser(User user) {
        //获取数据库的连接
        Connection conn = JDBCUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int affNum = 0;
        try {
            //写sql
            String sql = "insert into sm_user( username, password,pow) values(?,?,?);";

            //获取sql执行对象
            pstmt = conn.prepareStatement(sql);
            //设置参数
            pstmt.setString(1,user.getUsername());
            pstmt.setString(2,user.getPassworld());
            pstmt.setString(3,user.getPow().toString());

            //执行sql
            affNum = pstmt.executeUpdate();

        } catch (SQLException e) {
            LOGGER.error(e.getMessage());
        }

        //释放资源
        JDBCUtil.close(rs,pstmt,conn);
        return affNum>0;
    }

    @Override
    public List<User> findAllUser() {
        //获取数据库的连接
        Connection conn = JDBCUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<User> list = new ArrayList<>();
        try {
            //写sql
            String sql = "select * from tb_user";

            //获取sql执行对象
            pstmt = conn.prepareStatement(sql);

            //执行sql
            rs = pstmt.executeQuery();

            //处理结果
            User user;
            while (rs.next()) {
                user = new User();
                String UserName = rs.getString("username");
                String password = rs.getString("password");
                String pow = rs.getString("pow");

                user.setUsername(UserName);
                user.setPassworld(password);
                Pow pows = Pow.valueOf(pow.toUpperCase());
                user.setPow(pows);
                list.add(user);
            }
            //System.out.println(brandList);
        } catch (SQLException e) {
            LOGGER.error(e.getMessage());
        }

        //释放资源
        JDBCUtil.close(rs,pstmt,conn);
        return list;
    }

    @Override
    public boolean deleteUser(String delId, Pow pow) {
        //获取数据库的连接
        Connection conn = JDBCUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int affNum = 0;
        try {
            //写sql
            String sql = "delete from sm_user where username = ?";

            //获取sql执行对象
            pstmt = conn.prepareStatement(sql);
            //设置参数
            pstmt.setString(1,delId);


            //执行sql
            affNum = pstmt.executeUpdate();

            /*//处理结果
            System.out.println(affNum > 0);*/
        } catch (SQLException e) {
            LOGGER.error(e.getMessage());
        }

        //释放资源
        JDBCUtil.close(rs,pstmt,conn);
        return affNum > 0;
    }

    @Override
    public boolean updateUser(String updateId, User newUser) {
        //获取数据库的连接
        Connection conn = JDBCUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int affNum = 0 ;
        try {
            //写sql
            String sql = "UPDATE `sm_user` SET `password` = ?, `pow` = ? WHERE `username` = ?;\n";

            //获取sql执行对象
            pstmt = conn.prepareStatement(sql);
            //设置参数
            pstmt.setString(1,newUser.getPassworld());
            pstmt.setString(2,newUser.getPow().toString());
            pstmt.setString(3,newUser.getUsername());


            //执行sql
            affNum = pstmt.executeUpdate();

            /*//处理结果
            System.out.println(affNum > 0);*/
        } catch (SQLException e) {
            LOGGER.error(e.getMessage());
        }

        //释放资源
        JDBCUtil.close(rs,pstmt,conn);
        return affNum > 0;
    }

    @Override
    public int getIndex(String id) {

        return 0;
    }

    @Override
    public User getUser(String uname) {
        //获取数据库的连接
        Connection conn = JDBCUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            //写sql
            String sql = "select * from tb_user where username = ?";

            //获取sql执行对象
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,uname);
            System.out.println(sql);
            //执行sql
            rs = pstmt.executeQuery();

            //处理结果
            User user;
            if (rs.next()) {
                user = new User();
                String UserName = rs.getString("username");
                String password = rs.getString("password");
                String pow = rs.getString("pow");

                user.setUsername(UserName);
                user.setPassworld(password);
                Pow pows = Pow.valueOf(pow.toUpperCase());
                user.setPow(pows);
                return user;
            }
            //System.out.println(brandList);
        } catch (SQLException e) {
            LOGGER.error(e.getMessage());
        }

        //释放资源
        JDBCUtil.close(rs,pstmt,conn);
        return null;
    }
}
